package sws.alex.run;

import android.view.MotionEvent;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.content.Context;

public class GameView extends View implements Runnable{
	private Bitmap playerBitmap;
	private Bitmap bulletBitmap;
	private Bitmap enemyBitmap;
	
	private Player player;
	private Bullet[] bullets = new Bullet[20];
	private Enemy enemy;
	
	private Context context;
	public int screenWidth,screenHeight;
	public Bitmap exploreBitmap;
	private boolean isExplore;
	private int tick = 0;
	private int previousX,previousY;
	private final int FREQUENT_PAINT = 1000/60;
	private final int FREQUENT_BULLET = 30;
	
	public GameView(Context context){
		super(context);
	}

	public GameView(Context context,int screenWidth, int screenHeight){
		super(context);
		this.context = context;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		
		setFocusable(true);
	}

	@Override
	public void draw(Canvas canvas){
		player.paint(canvas);
		for(int i=0;i<bullets.length;i++){
			bullets[i].draw(canvas);
		}
		enemy.draw(canvas);
		
		if(isExplore){
			canvas.drawBitmap(exploreBitmap, enemy.getX(), enemy.getY(),null);
			isExplore = false;
			enemy.setAlive(false);
		}
		
		super.draw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent e){
		int x = (int)e.getX();
		int y = (int)e.getY();
		
		switch(e.getAction()){
		case MotionEvent.ACTION_MOVE:
			player.move(x-previousX,y-previousY);
			break;
		}
		previousX = x;
		previousY = y;
		
		return true;
	}

	@Override
	public void run() {
		load();
		init();
		while(true){
			if(!enemy.isAlive()){
				enemy.setAlive(true);
			}				
			tick();
			postInvalidate();
			try{
				Thread.sleep(FREQUENT_PAINT);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
	
	public void load(){
		playerBitmap = GameHelper.getBitmap(context,R.drawable.player);
		player = new Player(playerBitmap, playerBitmap.getWidth(), playerBitmap.getHeight());
		player.setScreenSize(screenWidth, screenHeight);
		
		bulletBitmap = GameHelper.getBitmap(context, R.drawable.bullet);
		exploreBitmap = GameHelper.getBitmap(context, R.drawable.explore);
		for(int i=0;i<bullets.length;i++){
			bullets[i] = new Bullet(bulletBitmap, bulletBitmap.getWidth(),bulletBitmap.getHeight());
			bullets[i].setScreenSize(screenWidth, screenHeight);
		}
		
		enemyBitmap = GameHelper.getBitmap(context,R.drawable.enemy);
		enemy = new Enemy(enemyBitmap, enemyBitmap.getWidth(), enemyBitmap.getHeight());
		enemy.setScreenSize(screenWidth, screenHeight);
	}
	
	public void init(){
		player.init();
		for(int i=0;i<bullets.length;i++){
			bullets[i].setAlive(false);
		}
	}
	
	public void tick(){
		if(tick == 0){
			for(int i=0;i<bullets.length;i++){
				if(bullets[i].getAlive()==false){
					bullets[i].setAlive(true, 
						player.getRefPixelX()-bullets[i].getWidth()/2, 
						player.getRefPixelY()-player.getHeight());
					break;
				}
			}
		}
		tick++;
		tick = tick % FREQUENT_BULLET;
		
		for(int i=0;i<bullets.length;i++){
			if(bullets[i].getAlive()==true){
				bullets[i].move();
				if(bullets[i].collidesWith(enemy, false)){
					isExplore = true;
					bullets[i].setAlive(false);
				}
			}
		}
		enemy.move();
	}

	public void start(){
		new Thread(this).start();
	}
	
}
